package com.comment.controller;

import com.alibaba.fastjson.JSON;
import com.comment.annotation.AccessLimit;
import com.comment.annotation.JwtIgnore;
import com.comment.api.BlogCommentApi;
import com.comment.common.RestfulResult;
import com.comment.common.ResultCode;
import com.comment.config.model.Audience;
import com.comment.dto.CommentDTO;
import com.comment.dto.CommentListDTO;
import com.comment.dto.CommentRequest;
import com.comment.service.CommentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author vedeng
 * @className CommentController
 * @description:
 * @date 2021/12/13 14:26
 */
@RestController
public class CommentController implements BlogCommentApi {

    private static final Logger logger = LoggerFactory.getLogger(CommentController.class);

    @Autowired
    private Audience audience;


    @Autowired
    private CommentService commentService;


    @JwtIgnore
    @RequestMapping("/index")
    public ModelAndView index(){
        CommentRequest commentRequest = new CommentRequest();
        CommentListDTO commentListDTO = commentService.queryAllComment(commentRequest);
        ModelAndView mv = new ModelAndView();
        mv.addObject("commentDTOList",commentListDTO.getCommentDTOList());
        mv.addObject("total",commentListDTO.getTotal());
        mv.addObject("pageNum", commentListDTO.getPageNum());
        mv.setViewName("page/comment/comment");
        return mv;
    }


    @Override
    @AccessLimit(seconds = 10, maxCount = 1)
    @JwtIgnore
    public RestfulResult addComment(@RequestBody CommentRequest commentRequest) {
        logger.info("CommentController addComment：{}", JSON.toJSONString(commentRequest));
        //获取用户id
        try {
            commentService.addComment(commentRequest);
        } catch (Exception e) {
            logger.error("添加评论出错",e);
            return RestfulResult.fail(ResultCode.SYSTEM_INNER_ERROR.getCode(), ResultCode.SYSTEM_INNER_ERROR.getMessage());
        }
        return RestfulResult.success(ResultCode.ADD_COMMENT_SUCCESS);
    }

    @Override
    @JwtIgnore
    public List<CommentDTO> queryAllComment(CommentRequest commentRequest) {
        logger.info("CommentController queryAllComment：{}", JSON.toJSONString(commentRequest));
        return commentService.queryAllCommentByBlogId(commentRequest);
    }




    /**
     * @description: 查询所有评论信息
     * @param: [commentRequest, request]
     * @return: org.springframework.web.servlet.ModelAndView
     * @author vedeng
     * @date: 2022/2/9 14:32
     */
    @JwtIgnore
    @RequestMapping("/queryAllComment")
    public ModelAndView queryAllComment(@RequestBody CommentRequest commentRequest, HttpServletRequest request){

        CommentListDTO commentListDTO = commentService.queryAllComment(commentRequest);
        ModelAndView mv = new ModelAndView();
        mv.addObject("commentDTOList",commentListDTO.getCommentDTOList());
        mv.addObject("total",commentListDTO.getTotal());
        mv.addObject("pageNum", commentListDTO.getPageNum());
        mv.setViewName("page/comment/comment");
        return mv;

    }


    @JwtIgnore
    @RequestMapping("/replyComment")
    public RestfulResult replyComment(@RequestBody CommentRequest commentRequest, HttpServletRequest request){

        commentService.replyComment(commentRequest,1);
        return RestfulResult.success(ResultCode.ADD_COMMENT_SUCCESS);
    }


}